package fiscal.dao;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Query;

import exceptions.PersistenceExceptionMessage;
import fiscal.entities.tables.Classificacao;
import generic.dao.GenericDAO;

public class PersistenceClassificacao extends GenericDAO<Classificacao, String> {

	@Override
	public Class<Classificacao> getCurrentClass() {
		return Classificacao.class;
	}
	@SuppressWarnings("unchecked")
	public Collection<Classificacao> findAllCapitulosPosicoes() throws PersistenceExceptionMessage {
		if (getEntityManager() != null) {
			Query query = getEntityManager().createQuery("FROM " +getCurrentClass().getName() +" c WHERE LENGTH(c.id) = :numero ORDER BY c.id");
			query.setParameter("numero", 4);
		
			return query.getResultList();
		}
		return new ArrayList<Classificacao>();
	}
	@SuppressWarnings("unchecked")
	public Collection<Classificacao> findChildrens(Classificacao ncm) throws PersistenceExceptionMessage {
		if (getEntityManager() != null) {
			if (ncm != null && ncm.getId() != null && ncm.getId().length() >= 4) {
				Query query = getEntityManager().createQuery("FROM " +getCurrentClass().getName() +" c WHERE " +
						" c.pai.id = :pai ORDER BY c.id");
			
				query.setParameter("pai", ncm.getId());
				return query.getResultList();
			}
		}
		return new ArrayList<Classificacao>();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Classificacao> findByDescricaoLike(String descricao) throws PersistenceExceptionMessage {
		if (getEntityManager() != null) {
			Query query = getEntityManager().createQuery("FROM " +getCurrentClass().getName() +" c WHERE " +
					" UPPER(c.descricao) LIKE UPPER(:descricao) ORDER BY c.id");
		
			query.setParameter("descricao", descricao +"%");
		
			return query.getResultList();
		}
		return new ArrayList<Classificacao>();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Classificacao> findByIdLike(String id) throws PersistenceExceptionMessage {
		if (getEntityManager() != null) {
			Query query = getEntityManager().createQuery("FROM " +getCurrentClass().getName() +" c WHERE " +
					" c.id LIKE :id ORDER BY c.id");
		
			query.setParameter("id", id +"%");
		
			return query.getResultList();
		}
		return new ArrayList<Classificacao>();
	}
	
	@SuppressWarnings("unchecked")
	public Classificacao findPai(Classificacao ncm) throws PersistenceExceptionMessage {
		if (getEntityManager() != null) {
			StringBuffer strBuf = new StringBuffer();
			boolean primeiro = true;
			for (String str : ncm.getPais()) {
				if (primeiro) {
					strBuf.append("c.id = :idPai" +str);
					primeiro = !primeiro;
				} else
					strBuf.append(" OR c.id = :idPai" +str);
			}
			
			Query query = getEntityManager().createQuery("FROM " 
					+getCurrentClass().getName() +" c WHERE " +strBuf.toString() +" ORDER BY c.id DESC");
			
			for (String str : ncm.getPais()) {
				query.setParameter("idPai" +str, str);
			}
			
			ArrayList<Classificacao> ncms = (ArrayList<Classificacao>) query.getResultList();
			if (ncms.size() > 0)
				return ncms.get(0);
			return null;
		}
		return null;
	}
	public void mergePais() throws PersistenceExceptionMessage {
		ArrayList<Classificacao> classificacaos = (ArrayList<Classificacao>) findAll();
		for (Classificacao classificacao : classificacaos) {
			classificacao.setPai(findPai(classificacao));
			classificacao.merge();
		}
	}
}
